{% extends 'base.html' %}
{% block title %}
    <a class="navbar-brand" href="#"> 员工出差管理 </a>
{% endblock %}
{% block write %}
    <li>
    <a  method="post" href="{{ url_for('evection_add',userID=current_user.userID) }}" class="btn btn-simple btn-danger btn-icon remove">
        申请出差
    </a>
    </li>
{% endblock %}
{% block content %}
{# 使用 length 过滤器获取 staffs 变量的长度 #}
    {% if current_user.has_role('Admin') or current_user.has_role('Root') %}

    {% endif %}
    {% if current_user.has_role('Admin') or current_user.has_role('Root') %}
    <div class="content">
        {% if evections == [] %}
            <h4 class="title">暂无员工出差信息</h4>
        {% else %}
            <h4 class="title">员工出差信息</h4>
        {% endif %}
        <div class="toolbar">

            <!--        Here you can write extra buttons/actions for the toolbar              -->
            <form method="post">
                查询方式
                <select name="info" required>
                    <option value="userID">员工号</option>
                    <option value="username">员工姓名</option>
                    <option value="departmentID">部门编号</option>
                    <option value="departmentname">部门名称</option>
                </select>
                <input type="text" name="date" autocomplete="off" required>
                <input type="submit" name="submit" value="查询">
            </form>

        </div>
        {% if info == 'userID' or info == 'username' %}
            <div class="material-datatables">
                <table id="datatables" class="table table-striped table-no-bordered table-hover"
                       style="width:100%;cellspacing:0">
                    <thead>
                    <tr>
                        <th>员工号</th>
                        <th>姓名</th>
                        <th>部门</th>
                        <th>岗位</th>
                        <th>状态</th>
                        <th class="disabled-sorting">操作</th>
                    </tr>
                    </thead>

                    <tbody>

                    {% for evection in evections %}
                        {% if evection.state=='审核中' %}
                            <tr>
                                <td>{{ staffs.userID }}</td>
                                <td>{{ staffs.username }}</td>
                                <td>{{ departments.departmentname }}</td>
                                <td>{{ posts.postname }}</td>
                                <td>{{ evection.state }}</td>
                                <td>
                                    <a href="{{ url_for('evection_staff',userID=staffs.userID,id=evection.id) }}"
                                       class="btn btn-simple btn-info btn-icon like">详情</a>
                                    <a href="{{ url_for('evection_change',userID=staffs.userID,id=evection.id) }}"
                                       class="btn btn-simple btn-warning btn-icon edit">修改</a>
                                    <a href="{{ url_for('evection_state',userID=staffs.userID,id=evection.id) }}"
                                       class="btn btn-simple btn-danger btn-icon remove">审核</a>
                                </td>
                            </tr>
                        {% endif %}
                    {% endfor %}
                    {% for evection in evections %}
                        {% if evection.state != '审核中' %}
                            <tr>
                                <td>{{ staffs.userID }}</td>
                                <td>{{ staffs.username }}</td>
                                <td>{{ departments.departmentname }}</td>
                                <td>{{ posts.postname }}</td>
                                <td>{{ evection.state }}</td>
                                <td>
                                    <a href="{{ url_for('evection_staff',userID=staffs.userID,id=evection.id) }}"
                                       class="btn btn-simple btn-info btn-icon like">详情</a>
                                </td>
                            </tr>
                        {% endif %}
                    {% endfor %}
                    </tbody>

                </table>
            </div>
        {% else %}

                <div class="material-datatables">
                    <table id="datatables" class="table table-striped table-no-bordered table-hover"
                           style="width:100%;cellspacing:0">
                        <thead>
                        <tr>
                            <th>员工号</th>
                            <th>姓名</th>
                            <th>部门</th>
                            <th>岗位</th>
                            <th>状态</th>
                            <th class="disabled-sorting">操作</th>
                        </tr>
                        </thead>

                        <tbody>
                        {% for evection in evections %}
                        {% for staff in staffs %}
                        <tr>
                        {% if staff.userID == evection.userID and evection.state == '审核中' %}
                            <td>{{ staff.userID }}</td>
                            <td>{{ staff.username }}</td>
                            {% if info == 'departmentID' or info == 'departmentname' %}
                                <td>{{ departments.departmentname }}</td>
                            {% else %}
                                {% for department in departments %}
                                    {% if staff.departmentID == department.departmentID %}
                                        <td>{{ department.departmentname }}</td>
                                    {% endif %}
                                {% endfor %}
                            {% endif %}
                            {% for post in posts %}
                                {% if staff.postID == post.postID %}
                                    <td>{{ post.postname }}</td>
                                {% endif %}
                            {% endfor %}
                            <td>{{ evection.state }}</td>
                            <td>
                                <a href="{{ url_for('evection_staff',userID=staff.userID,id=evection.id) }}"
                                   class="btn btn-simple btn-info btn-icon like">详情</a>
                                <a href="{{ url_for('evection_change',userID=staff.userID,id=evection.id) }}"
                                   class="btn btn-simple btn-warning btn-icon edit">修改</a>
                                <a href="{{ url_for('evection_state',userID=staff.userID,id=evection.id) }}"
                                   class="btn btn-simple btn-danger btn-icon remove">审核</a>
                            </td>
                        {% endif %}
                        {% endfor %}
                        {% endfor %}
                        {% for evection in evections %}
                        {% for staff in staffs %}
                        {% if staff.userID == evection.userID and evection.state != '审核中' %}
                            <td>{{ staff.userID }}</td>
                            <td>{{ staff.username }}</td>
                            {% if info == 'departmentID' or info == 'departmentname' %}
                                <td>{{ departments.departmentname }}</td>
                            {% else %}
                                {% for department in departments %}
                                    {% if staff.departmentID == department.departmentID %}
                                        <td>{{ department.departmentname }}</td>
                                    {% endif %}
                                {% endfor %}
                            {% endif %}
                            {% for post in posts %}
                                {% if staff.postID == post.postID %}
                                    <td>{{ post.postname }}</td>
                                {% endif %}
                            {% endfor %}
                            <td>{{ evection.state }}</td>
                            <td>
                                <a href="{{ url_for('evection_staff',userID=staff.userID,id=evection.id) }}"
                                   class="btn btn-simple btn-info btn-icon like">详情</a>
                            </td>
                        {% endif %}
                        </tr>
                        {% endfor %}
                        {% endfor %}
                        </tbody>

                    </table>
                </div>


        {% endif %}
    </div>
    {% elif current_user.has_role('User') %}
    <div class="material-datatables">
        <table id="datatables" class="table table-striped table-no-bordered table-hover"
               style="width:100%;cellspacing:0">
            <thead>
            <tr>
                <th>员工号</th>
                <th>姓名</th>
                <th>部门</th>
                <th>岗位</th>
                <th>状态</th>
                <th class="disabled-sorting">操作</th>
            </tr>
            </thead>

            <tbody>
            {% for evection in evections %}
            {% if evection.state == '审核中' %}
            <tr>
                <td>{{ staffs.userID }}</td>
                <td>{{ staffs.username }}</td>
                <td>{{ departments.departmentname }}</td>
                <td>{{ posts.postname }}</td>
                <td>{{ evection.state }}</td>
                <td>
                    <a href="{{ url_for('evection_staff',userID=staffs.userID,id=evection.id) }}"
                       class="btn btn-simple btn-info btn-icon like">详情</a>
                    <a href="{{ url_for('evection_change',userID=staffs.userID,id=evection.id) }}"
                       class="btn btn-simple btn-warning btn-icon edit">修改</a>
                </td>
            </tr>
            {% endif %}
            {% endfor %}
            {% for evection in evections %}
            {% if evection.state != '审核中' %}
                <tr>
                    <td>{{ staffs.userID }}</td>
                    <td>{{ staffs.username }}</td>
                    <td>{{ departments.departmentname }}</td>
                    <td>{{ posts.postname }}</td>
                    <td>{{ evection.state }}</td>
                    <td>
                        <a href="{{ url_for('evection_staff',userID=staffs.userID,id=evection.id) }}"
                           class="btn btn-simple btn-info btn-icon like">详情</a>
                    </td>
                </tr>
            {% endif %}
            {% endfor %}
            </tbody>

        </table>
    </div>
    {% endif %}
{% endblock %}